package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.dao.CourseDao;
import com.resourcestore.dao.MajorDao;
import com.resourcestore.dao.CourseCategoryDao;
import com.resourcestore.model.Course;
import com.resourcestore.model.CourseCategory;
import com.resourcestore.model.Major;
import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;

public class CourseAction extends BaseAction {

	private Course course;
	private List<Course> listCourse;
	private CourseDao courseDao = new CourseDao();
	private Major major;
	private List<Major> preInsert;
	private MajorDao majorDao = new MajorDao();
	private CourseCategory courseCategory;
	private List<CourseCategory> preInsertCategory;
	private CourseCategoryDao courseCategoryDao = new CourseCategoryDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listCourse() {

		course = new Course();
		course.setIsPublic(-1);
		pagination.setTotal(courseDao.queryCourseNum(course));
		listCourse = courseDao.selectCoursePage(course, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourse.size(); i++) {
			if (listCourse.get(i).getIsPublic() == 0) {
				listCourse.get(i).setIsPublicString("否");
			} else if (listCourse.get(i).getIsPublic() == 1) {
				listCourse.get(i).setIsPublicString("是");
			}
			try {
				listCourse.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourse.get(i)
								.getGmtCreated()));
				listCourse.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourse.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";

	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListCourse() {

		// 对内容进行urldecoder解码
		try {
			course.setMajorName(new String(course.getMajorName().getBytes(
					"iso-8859-1"), "UTF-8"));
			course.setCourseCateName(new String(course.getCourseCateName()
					.getBytes("iso-8859-1"), "UTF-8"));
			course.setCourseName(new String(course.getCourseName().getBytes(
					"iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(courseDao.queryCourseNum(course));
		listCourse = courseDao.selectCoursePage(course, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listCourse.size(); i++) {
			if (listCourse.get(i).getIsPublic() == 0) {
				listCourse.get(i).setIsPublicString("否");
			} else if (listCourse.get(i).getIsPublic() == 1) {
				listCourse.get(i).setIsPublicString("是");
			}
			try {
				listCourse.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourse.get(i)
								.getGmtCreated()));
				listCourse.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourse.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/Course/listCourse.jsp");
	}

	/**
	 * 预处理插入信息
	 */

	public String preInsertCourse() {
		preInsert = majorDao.selectMajor();

		//preInsertCategory = courseCategoryDao.selectCourseCategory();

		return newpath("/Course/insertCourse.jsp");

	}

	/**
	 * 插入一条数据
	 */
	public String insertCourse() {

		try {
			course.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			course.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		courseDao.insertCourse(course);
		return redirect("/Course/listCourse.action");
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourse() {

		course = courseDao.selectCourseById(course);

		preInsert = majorDao.selectMajor();
//
//		preInsertCategory = courseCategoryDao.selectCourseCategory();

		return newpath("/Course/updateCourse.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateCourse() {

		try {
			// course.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			course.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		courseDao.updateCourse(course);

		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourse() {
		courseDao.deleteCourse(course);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourse() {
	
		courseDao.deleteMoreCourse(course);
		return redirect(this.getUrl());
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public List<Course> getListCourse() {
		return listCourse;
	}

	public void setListCourse(List<Course> listCourse) {
		this.listCourse = listCourse;
	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}

	public List<Major> getPreInsert() {
		return preInsert;
	}

	public void setPreInsert(List<Major> preInsert) {
		this.preInsert = preInsert;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public List<CourseCategory> getPreInsertCategory() {
		return preInsertCategory;
	}

	public void setPreInsertCategory(List<CourseCategory> preInsertCategory) {
		this.preInsertCategory = preInsertCategory;
	}

	public CourseCategory getCourseCategory() {
		return courseCategory;
	}

	public void setCourseCategory(CourseCategory courseCategory) {
		this.courseCategory = courseCategory;
	}

}
